<?php
namespace Admin\Model;
use Admin\Model\BaseModel;
class AdminRoleModel extends BaseModel
{
    public $tableName = 'admin_role';
    public $_validate = array(
        array('name', '1,100', '角色名必须在1~100之间', 0, 'length', 3),
        array('name', '', '角色名已存在', 0, 'unique', 3),
    );

    public function getUserRoleInfo($id)
    {
        $roleInfo = $this->where(array('id' => $id))->find();
        if (false === $roleInfo) {
            return array();
        }
        return $roleInfo;
    }

    /**
     * 角色分页列表
     *
     */
    public function getNewsPageList($where, $order = '', $nowPage = 1, $pageSize = 10)
    {
        $count = $this->where($where)->count();
        $Page = new \Admin\Util\Page($count, $pageSize);
        $data['list'] = $this->where($where)->order($order)->page($nowPage . ',' . $Page->listRows)->select();
        $data['page'] = $Page->show();
        return $data;
    }

    /**
     * 得到所有角色列表
     * @param type $where
     * @return type
     */
    public function getRoleAllList()
    {
        $roleList = $this->select();
        if (false === $roleList) {
            return array();
        }
        $permissionAtom = new \Admin\Model\AdminAtomModel();
        foreach ($roleList as $key => $value) {
            $roleList[$key]['atomName'] = $permissionAtom->getAtomName4Str($value['atom_list']);
        }
        return $roleList;
    }

    /**
     * 保存数据
     * @param type $dataArray
     * @return type
     * @throws Exception
     */
    public function doSave($dataArray)
    {
        if (empty($dataArray['id'])) {
            $dataArray['create_time'] = date("Y-m-d H:i:s");
        }
        $dataArray['update_time'] = date("Y-m-d H:i:s");

        if (false === $this->create($dataArray)) {
            throw new \Exception($this->getError());
        }

        if (empty($dataArray['id'])) {
            $res = $this->add($dataArray);
        } else {
            $res = $this->save($dataArray);
        }
        return $res;
    }

    /**
     * 删除信息
     * @param type $id
     * @return type
     */
    public function delRole($id)
    {
        //故意查询一下，如果没有info缓存,_after_del方法会执行不正常
        $roleInfo = $this->getUserRoleInfo($id);
        if (empty($roleInfo)) {
            return TRUE;
        }
        $where['id'] = $id;
        $res = $this->where($where)->delete();
        return $res;
    }

    /**
     * 将一串“，”号隔开的字符，分解成对应的角色详情
     * @param String $roleStr
     * @return array
     */
    public function getRole4Str($roleStr)
    {
        $roleList = array();
        $roleStr = str_replace('，', ',', $roleStr);
        $roleArray = explode(',', $roleStr);
        foreach ($roleArray as $key => $value) {
            $roleList[$value] = $this->getUserRoleInfo($value);
        }
        return $roleList;
    }

    /**
     * 将一串“，”号隔开的字符，得到权限名字
     * @param String $roleStr
     * @return array
     */
    public function getRoleName4Str($roleStr)
    {
        $roleList = $this->getRole4Str($roleStr);
        $roleName = '';
        foreach ($roleList as $key => $value) {
            $roleName .= $value['name'] . ',';
        }
        return substr($roleName, 0, -1);
    }


    protected function _after_update($data, $options)
    {

    }

    protected function _after_delete($data, $options)
    {

    }

    protected function _after_insert($data, $options)
    {

    }
}

?>